#include<iostream>
using namespace std;
struct FlistNode
{
		int val;
			FlistNode *next;
				FlistNode(const int x) :val(x), next(0){}
};
//剑指offer题14：输入一个链表，输出该链表中倒数第k个结点。
FlistNode *SingLinkList(FlistNode * head, size_t k)
{
	FlistNode *slow = head, *fast = head;
	int count = 0;//用来计算链表中到底有多少个结点
	while (fast){ ++count; fast = fast->next; }
	k %= count + 1;//ps，如果k的值大于count时，修正k的值是十分有必要的，
	fast = head;
	while (k--)fast = fast->next;//这里不用判断fast是否为空,因为前面修正了k的值
	while (fast)
	{
		slow = slow->next;
		fast = fast->next;
	}
	return slow;
}
int main()
{
	FlistNode* head1 = new FlistNode(1);
	FlistNode* head2 = new FlistNode(2); head1->next = head2;
	FlistNode* head3 = new FlistNode(3); head2->next = head3;
	FlistNode* head4 = new FlistNode(4); head3->next = head4;
	FlistNode* head5 = new FlistNode(5); head4->next = head5;
	FlistNode* head6 = new FlistNode(6); head5->next = head6;
	FlistNode* tmp = SingLinkList(head1, 12);

	if (tmp)
		cout << tmp->val << endl;
	return 0;
}
